# include "Obstacle.h"

# include "Renderer.h"

Obstacle::Obstacle()
{

}

Obstacle::~Obstacle()
{
	if ( myEntity )
	{
		myRenderer->deleteEntity( myEntity);
	}

	if ( mySceneNode )
	{
		myRenderer->deleteSceneNode( mySceneNode);
	}
}

void Obstacle::setPosition( const Ogre::Vector3& position)
{
	mySceneNode->setPosition( position);
}

void Obstacle::init( Renderer* renderer, const std::string& lua)
{
	myRenderer = renderer;
	myLua = lua;
}

void Obstacle::load( const std::string& name)
{
	lua_State* stateTmp = lua_open();
	luaL_openlibs( stateTmp);
	luaL_dofile( stateTmp, myLua.c_str());
	lua_settop( stateTmp, 0);
	lua_getglobal( stateTmp, name.c_str());

	lua_pushnumber( stateTmp, 1);
	lua_gettable( stateTmp, -2);
	myEntity = myRenderer->createEntity( "house", lua_tostring( stateTmp, -1));
	lua_pop( stateTmp, 2);
	myEntity->setQueryFlags( WALL_FLAG);
	mySceneNode = myRenderer->createSceneNode( "houseNode");
	mySceneNode->showBoundingBox( true);
	mySceneNode->attachObject( myEntity);

	lua_close( stateTmp);
}